<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/23 0023
 * Time: 下午 1:07
 * Author: NanTao <260053530@qq.com>
 */

namespace app\index\controller;

use think\Db;
class Cart extends Common
{
    public function index()
    {
        $userId = session('userId');
        $list = Db::name('cart')->where('user_id',$userId)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function add()
    {
        $userId = session('userId');
        $goodsId = input('param.goodsId');
        // $goodsCount = input('param.goodsCount');
        $goodsCount = 1;
        $type = input('param.type');
        if ($goodsCart = Db::name('cart')->where(['user_id'=>$userId,'goods_id'=>$goodsId])->find())
        {
            if (isset($type) && $type=='now')
            {
                $result = Db::name('cart')->where(['user_id'=>$userId,'goods_id'=>$goodsId])->setField('goods_count',$goodsCount);
            } else {
                $result = Db::name('cart')->where(['user_id'=>$userId,'goods_id'=>$goodsId])->setField('goods_count',$goodsCart['goods_count']+$goodsCount);
            }
            $id = $goodsCart['id'];
        }
        else {
            $goods = Db::name('goods')->field('title,sell_price,img_url')->where('id',$goodsId)->find();
            $data = [
                'user_id'=>$userId,
                'goods_id'=>$goodsId,
                'goods_count'=>$goodsCount,
                'goods_title'=>$goods['title'],
                'goods_sell_price'=>$goods['sell_price'],
                'goods_img_url' =>$goods['img_url']
            ];
            $result = Db::name('cart')->insert($data);
            $id = Db::name('cart')->getLastInsID();
        }
        if ($result || $result==0)
        {
            json_msg_data(0,'成功加入购物车',$id);
        } else {
            json_msg(1,'加入失败');
        }
    }

    public function del()
    {
        $cartId = input('param.cartId');
        if (Db::name('cart')->delete($cartId)) {
            json_msg(0, '删除成功');
        } else {
            json_msg(1, '系统错误,删除失败');
        }
    }

    /*
    * 购物车商品数量加减
    * */
    public function goodsCount()
    {
        $cartId = input('param.cartId');
        $action = input('param.action');
        switch ($action) {
            case 'jia':
                $model = Db::name('cart');
                $cart = $model->field('goods_count,goods_id')->find($cartId);
                $goodsCount = Db::name('goods')->where('id', $cart['goods_id'])->value('stock');
                if ($goodsCount <= $cart['goods_count']) {
                    json_msg(1,'商品库存不足');
                }
                $result = $model->where('id', $cartId)->setInc('goods_count');
                break;
            case 'jian':
                $goodsCount = Db::name('cart')->where('id', $cartId)->value('goods_count');
                if ($goodsCount == 1) {
                    $cart = Db::name('cart')->where('id', $cartId)->find();
                    $data = [
                        'goods_price_sum' => $cart['goods_sell_price'] * $cart['goods_count'],
                        'goods_count'=>$cart['goods_count']
                    ];
                    json_msg_data(0,'操作成功',$data);
                } else {
                    $result = Db::name('cart')->where('id', $cartId)->setDec('goods_count');
                }
                break;
        }
        if ($result) {
            $cart = Db::name('cart')->where('id', $cartId)->find();
            $data = [
                'goods_price_sum' => $cart['goods_sell_price'] * $cart['goods_count'],
                'goods_count'=>$cart['goods_count']
            ];
            json_msg_data(0,'操作成功',$data);
        } else {
            json_msg(1,'系统错误,操作失败');
        }
    }

    public function nowAdd()
    {
        $userId = session('userId');
        $goodsId = input('param.goodsId');
        $goodsCount = input('param.goodsCount');
        if ($goodsCart = Db::name('cart')->where(['user_id'=>$userId,'goods_id'=>$goodsId])->find())
        {
            $result = Db::name('cart')->where(['user_id'=>$userId,'goods_id'=>$goodsId])->setField('goods_count',$goodsCount);
            $id = $goodsCart['id'];
        }
        else {
            $goods = Db::name('goods')->field('title,sell_price,img_url')->where('id',$goodsId)->find();
            $data = [
                'user_id'=>$userId,
                'goods_id'=>$goodsId,
                'goods_count'=>$goodsCount,
                'goods_title'=>$goods['title'],
                'goods_sell_price'=>$goods['sell_price'],
                'goods_img_url' =>$goods['img_url']
            ];
            $result = Db::name('cart')->insert($data);
            $id = Db::name('cart')->getLastInsID();
        }
        if ($result)
        {
            json_msg_data(0,'成功加入购物车',$id);
        } else {
            json_msg(1,'加入失败');
        }
    }
}